package com.woniuxy.tfss.filter;

import com.woniuxy.tfss.entity.Rider;
import com.woniuxy.tfss.entity.User;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter(filterName = "myFilter", urlPatterns = "/*")
public class MyFilter implements Filter {//简单的拦截器实现

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();
        HttpServletResponse resp = (HttpServletResponse) response;

        if (req.isRequestedSessionIdFromURL()) {
            if (session != null) session.invalidate();
        }

        User user = (User) session.getAttribute("user");
        Rider rider = (Rider) session.getAttribute("rider");
        int members = 0;
        if (user != null) {
            members++;
        }
        if (rider != null) {
            members++;
        }

        if (req.getRequestURI().contains("login")) {
            if (members == 0) {
                chain.doFilter(request, response);
                return;
            }
        } else if (req.getRequestURI().contains("logout")) {
            chain.doFilter(request, response);
            return;
        } else if (req.getRequestURI().contains("checkcode")) {
            chain.doFilter(request, response);
            return;
        } else if (req.getRequestURI().contains("admin")) {
            chain.doFilter(request, response);
            return;
        } else if (req.getRequestURI().contains("findPassword")) {
            chain.doFilter(request, response);
            return;
        } else if (req.getRequestURI().contains("register")) {
            chain.doFilter(request, response);
            return;
        } else if (req.getRequestURI().contains("create")) {
            if (user != null) {
                chain.doFilter(request, response);
                return;
            } else {
                ((HttpServletResponse) response).sendRedirect("/user/login");
                return;
            }
        } else if (req.getRequestURI().contains("confirm")) {
            if (user != null) {
                chain.doFilter(request, response);
                return;
            } else {
                ((HttpServletResponse) response).sendRedirect("/user/login");
                return;
            }
        } else if (req.getRequestURI().contains("user")) {
            if (user != null) {
                chain.doFilter(request, response);
                return;
            } else {
                ((HttpServletResponse) response).sendRedirect("/user/login");
                return;
            }
        } else {
            chain.doFilter(request, response);
        }
    }
}
